package cn.ylz.ylmall.product.controller;

import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.account.service.IUserService;
import cn.ylz.ylmall.common.web.JsonResult;
import cn.ylz.ylmall.product.pojo.param.PCommentAddNewParam;
import cn.ylz.ylmall.product.pojo.vo.PCommentStandardVO;
import cn.ylz.ylmall.product.service.IPCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Slf4j
@RequestMapping("/products/comments/")
@Api(tags = "3.2. 商品评论管理")
@Validated
public class CommentControler {
    @Autowired
    private IPCommentService pCommentService;

    @PostMapping("add-new")
    @ApiOperation(value = "发布评论")
    public JsonResult addNew(@Valid PCommentAddNewParam param,
                             @ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal, @ApiIgnore HttpServletRequest req) {
        pCommentService.addNew(param, currentPrincipal, req);
        return JsonResult.ok();
    }

    @GetMapping("{productId}/listComment")
    @ApiOperation(value = "查询评论列表")
    public JsonResult getAllComments(@PathVariable Long productId){
        return JsonResult.ok(pCommentService.findByProductId(productId));
    }

    @GetMapping("{commentId}/detail")
    @ApiOperation(value = "查询评论详情")
    public JsonResult commentDetailById(@PathVariable Long commentId){
        return JsonResult.ok(pCommentService.getStandardById(commentId));
    }

    @GetMapping("{commentId}/delete")
    @ApiOperation(value = "删除评论")
    public JsonResult delete(@PathVariable Long commentId,@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        pCommentService.delete(commentId,currentPrincipal);
        return JsonResult.ok();
    }

    @PostMapping("{commentId}/like")
    @ApiOperation(value = "当前登录用户给评论点赞")
    public JsonResult likeComment(@PathVariable Long commentId,@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal) {
        return JsonResult.ok(pCommentService.likeComment(commentId, currentPrincipal.getId()));
    }

    @GetMapping("{productId}/LikedComment")
    @ApiOperation(value = "根据商品id查询用户的已点赞评论")
    public JsonResult likeCommentByUserId(@PathVariable Long productId,@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        return JsonResult.ok(pCommentService.likeCommentByUserId(productId,currentPrincipal.getId()));
    }





}
